#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>


pid_t pid_child;

void Parent_sighandler(int signum)
{
    if (signum == SIGUSR1)
    {
        printf("let’s gogogo\n");
    }
    else if (signum == SIGUSR2)
    {
        printf("stop the bus\n");
    }
    else if (signum == SIGTSTP)
    {
        kill(pid_child, SIGUSR1); //给父进程法信号
    }
}

void child_sighandler(int signum)
{
    if (signum == SIGINT)
    {
        kill(getppid(), SIGUSR1); //给父进程法信号
    }
    else if (signum == SIGQUIT)
    {
        kill(getppid(), SIGUSR2); //给父进程法信号
    }
    else if (signum == SIGUSR1)
    {
        printf("please get off the bus\n");
        kill(pid_child, SIGKILL); //杀死子进程，父进程退出，避免程序逻辑出问题
        exit(0);                  //父进程退出
    }
                
}

int main(int argc, char const *argv[])
{
    pid_t pid = fork(); //获取子进程1的进程号
    if (pid < 0)        //错误
    {
        perror("fork");
        exit(-1);
    }
    else if (pid == 0) //子进程(售票员)
    {
        signal(SIGINT, child_sighandler);
        signal(SIGQUIT, child_sighandler);
        signal(SIGTSTP, SIG_IGN);
        signal(SIGUSR1, child_sighandler);

        while (1)
        {
            sleep(1);
        }
    }
    else //父进程(司机)
    {
        pid_child = pid;
        signal(SIGINT, SIG_IGN);
        signal(SIGUSR1, Parent_sighandler);

        signal(SIGQUIT, SIG_IGN);
        signal(SIGUSR2, Parent_sighandler);
        signal(SIGTSTP, Parent_sighandler);

        while (1)
        {
            sleep(1);
        }
    }

    return 0;
}
